<template>
 <div class="message">
   <div><h1 class="title">员工留言板</h1><small class="title2">写下你想说的话吧：</small></div>

                    <el-button type="primary" size ="small" icon="view" class="fb" @click='onAddMessage()'>发表</el-button>


   <div class="hr"></div>
    <template>
    <el-table
     v-if="tableData.length > 0"
      class="table"
      :data="tableData"
      style="width: 1100px">
      <el-table-column
        prop="name"
        label="姓名"
        width="120">
      </el-table-column>
      <el-table-column
        prop="describe"
        label="留言"
        width="700">
      </el-table-column>
      <el-table-column
        prop="date"
        label="日期">
      </el-table-column>
  
    </el-table>
         <el-row>
                <el-col :span="24">
                    <div class="pagination">
                        <el-pagination
                            v-if='paginations.total > 0'
                            :page-sizes="paginations.page_sizes"
                            :page-size="paginations.page_size"
                            :layout="paginations.layout"
                            :total="paginations.total"
                            :current-page.sync='paginations.page_index'
                            @current-change='handleCurrentChange'
                            @size-change='handleSizeChange'>
                        </el-pagination>
                    </div>
                </el-col>
            </el-row>
            
       <!-- 弹框页面 -->
        <dialogMessage :dialog='dialog' :form='form' @update="getmessage"></dialogMessage>
  </template>
 </div>

 
</template>


<script>
import DialogMessage from '../components/Dialogmessage.vue'
export default {
  name: 'message',
  data() {
    return {
      tableData: [],
      allTableData: [],
      dialog: {
        show: false,
        title: '',
        option: 'edit',
      },
      form: {
        name: '',
        describe: '',
      },
      paginations: {
        page_index: 1, // 当前位于哪页
        total: 0, // 总数
        page_size: 5, // 1页显示多少条
        page_sizes: [3, 5, 10, 15, 20], //每页显示多少条
        layout: 'total, sizes, prev, pager, next, jumper', // 翻页属性
      },
    }
  },
  components: {
    DialogMessage,
  },
  created() {
    this.getmessage()
  },
  methods: {
    getmessage() {
      // 获取表格数据
      this.$axios('/api/messages/').then((res) => {
        this.allTableData = res.data
        this.setPaginations()
      })
    },
    handleCurrentChange(page) {
      // 当前页
      let sortnum = this.paginations.page_size * (page - 1)
      let table = this.allTableData.filter((item, index) => {
        return index >= sortnum
      })
      // 设置默认分页数据
      this.tableData = table.filter((item, index) => {
        return index < this.paginations.page_size
      })
    },
    handleSizeChange(page_size) {
      // 切换size
      this.paginations.page_index = 1
      this.paginations.page_size = page_size
      this.tableData = this.allTableData.filter((item, index) => {
        return index < page_size
      })
    },
    setPaginations() {
      // 总页数
      this.paginations.total = this.allTableData.length
      this.paginations.page_index = 1
      this.paginations.page_size = 5
      // 设置默认分页数据
      this.tableData = this.allTableData.filter((item, index) => {
        return index < this.paginations.page_size
      })
    },
    onAddMessage() {
      this.dialog = {
        show: true,
        title: '发布留言',
        option: 'add',
      }
      this.form = {
        name: '',
        describe: '',
      }
    },
  },
}
</script>
<style scoped>
.table {
  margin-top: 30px;
  margin-left: 30px;
}
.message {
  overflow: hidden;
  float: left;
  width: 1200px;
}
.title {
  float: left;
  font-size: 40px;
  margin-left: 100px;
  margin-top: 35px;
}
.title2 {
  display: block;
  float: left;
  font-size: 18px;
  color: rgb(80, 77, 77);
  margin-top: 57px;
  margin-left: 10px;
}
.hr {
  border: 1px solid rgb(230, 227, 227);
  width: 1100px;
  float: left;
  margin-top: 10px;
  margin-left: 50px;
}
.fb {
  float: right;
  margin-right: 280px;
  margin-top: 50px;
}
.pagination {
  text-align: right;
  margin-top: 10px;
}
</style>